	package com.Page.exam.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.Page.common.PageData;
import com.Page.exam.dto.QueryTestDomain;
import com.Page.exam.dto.TestDTO;
import com.Page.exam.service.TestService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 *  
* Copyright: Copyright (c) 2019 YancyChong.All rights reserved.
* 
* @ClassName: ExamController.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: YancyChong
* @date: 2019年5月15日 下午8:37:21 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2019年5月15日     User           v1.0.0               修改原因
 */
//http://127.0.0.1:8080/swagger-ui.html#/exam-controller/updateUsingPUT
@RestController
@RequestMapping("/v1/open/test/manager")
@Api( tags = "试题操作相关API")
@Slf4j
public class ExamController {
	@Autowired
	private TestService testService;

	/**
	 * @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
	 * PATCH相当于SaveOrUpdate
	 */
//	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加试题", notes = "添加试题<br><br><b>@author YancyChong</b>")
    public TestDTO add(@ApiParam(value = "题干及选项", required = true)@RequestBody TestDTO testDto) {
    	log.debug("添加试题内容：{}", testDto);
    	testService.add(testDto);
        return testDto;
    }

    
    @PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "PUT", value = "修改试题", notes = "修改试题<br><br><b>@author YancyChong</b>")
    public TestDTO update(
    		@ApiParam(value = "题干ID", required = true) @PathVariable String id,
    		@ApiParam(value = "题干ID及选项ID是必须的", required = true)@RequestBody TestDTO testDto
    		) {
    	testDto.setId(id);
    	log.debug("修改试题内容：{}", testDto);
    	testService.update(testDto);
        return testDto;
    }

    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "根据ID查询试题及选项列表", notes = "根据ID查询试题及选项列表<br><br><b>@author YancyChong</b>")
    public TestDTO get(@ApiParam(value = "题干ID", required = true) @PathVariable String id) {
    	log.debug("获取试题内容ID：{}", id);
        return testService.getById(id);
    }

    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除试题及对应的选项", notes = "删除试题及对应的选项<br><br><b>@author YancyChong</b>")
    public void delete(@ApiParam(value = "题干ID", required = true) @PathVariable String id) {
    	log.debug("删除试题ID：{}", id);
        testService.delete(id);
    }
    
    
	@RequestMapping(value = "/page", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "分页查询题目信息", response = Void.class, notes = "分页查询题目信息<br><br><b>@author YancyChong</b>")
	public PageData<TestDTO> page(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "<b>选填:</b><br>keyWords:查询关键字<br>pageNumber:第几页<br>pageSize:每页数据的数目<br>") @RequestBody QueryTestDomain domain) {
		
		return testService.getTestPage(domain);
	}
}
